/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.breeze.parser;

import com.breeze.util.ColumnData;
import com.breeze.util.StringUtil;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author huateng
 */
public abstract  class DefaultParser implements Parser{
    
    private List<ColumnData> dataList;
    private String tableName;
    private static Properties prop=null;
    
    public static String getProperties(String name){
        if(prop==null){
            InputStream is=null;
            try {
                prop=new Properties();
                is = new FileInputStream("src/test/templateConfig.properties");
                prop.load(is);
                
                
            } catch (IOException ex) {
                Logger.getLogger(DefaultParser.class.getName()).log(Level.SEVERE, null, ex);
                return null;
            }  finally {
                try {
                    is.close();
                } catch (IOException ex) {
                    Logger.getLogger(DefaultParser.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return prop.getProperty(name);
    }
    

    public void setDataList(List<ColumnData> list) {
        this.dataList=list;
    }

    public void setTableName(String tableName) {
        this.tableName=tableName;
    }

    /**
     * @return the dataList
     */
    public List<ColumnData> getDataList() {
        return dataList;
    }

    /**
     * @return the tableName
     */
    public String getTableName() {
        return tableName;
    }
    
    public String getJavaClassName(){
        return StringUtil.getString(StringUtil.getHumpString(tableName.toLowerCase()));
    }
    
    protected  String parseConfig(String str){
        StringBuilder sb=new StringBuilder();
        while(str.indexOf("$%{")>-1){
            int index=str.indexOf("$%{");
            int end;
            for(end=index+3;end<str.length();end++){
                if(str.charAt(end)=='}')
                    break;
            }
            String name=str.substring(index+3,end);
            String value=getProperties(name);
            sb.append(str.substring(0,index)).append(value);
            str=str.substring(end+1);
        }
        sb.append(str);
        return sb.toString();
    }
    
}
